const md5 = require('md5');
const rp = require('request-promise');
const crypto = require('crypto');

module.exports = class extends think.Service {
  async create(orderSn, aiModel) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }
    try {
      // 获取 session
      // https://fbm-api.fbmms.cn
      const options = {
        method: 'POST',
        url: 'https://fbm-api.fbmms.cn/standard/b/aiInterview',
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: {
          orderTokens: [orderSn],
          aiModelToken: aiModel,
          expireTime: 0,
        },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async report(orderSn) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }
    try {
      const options = {
        method: 'POST',
        url: 'https://fbm-api.fbmms.cn/standard/b/aiInterview/result',
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: {
          orderTokens: [orderSn],
          closeAIEvaluation: false,
        },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async modify(data) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }
    try {
      const options = {
        method: 'POST',
        url: 'https://fbm-api.fbmms.cn/standard/b/aiInterview/aiScore/modify',
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: data,
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async detail(token) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }
    try {
      const options = {
        method: 'POST',
        url: `https://fbm-api.fbmms.cn/standard/b/aiModel/${token}`,
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: {},
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async upsert(data) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }
    try {
      const options = {
        method: 'POST',
        url: 'https://fbm-api.fbmms.cn/standard/b/aiModel/upsert',
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: data,
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async ability(page, size) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }

    try {
      const options = {
        method: 'POST',
        url: `https://fbm-api.fbmms.cn/standard/b/questionCollection/list`,
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: {
          page: parseInt(page),
          pageSize: parseInt(size),
        },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async abilityPut(data) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }

    try {
      const options = {
        method: 'POST',
        url: `https://fbm-api.fbmms.cn/standard/b/questionCollection/upsert`,
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: data,
      };
      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async abilityDel(token) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }

    try {
      const options = {
        method: 'POST',
        url: `https://fbm-api.fbmms.cn/standard/b/questionCollection/delete`,
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: { token: token },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async abilityDetail(token) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }

    try {
      const options = {
        method: 'POST',
        url: `https://fbm-api.fbmms.cn/standard/b/questionCollection`,
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: { tokens: [token] },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }

  async question(token) {
    let aiToken = await think.cache('aiToken');

    if (!aiToken) {
      const TokenSerivce = await think.service('index', 'admin').create();
      aiToken = TokenSerivce.accessToken;
      await think.cache('aiToken', TokenSerivce.accessToken, {
        timeout: 10 * 60 * 1000,
      });
    }
    try {
      const options = {
        method: 'POST',
        url: `https://fbm-api.fbmms.cn/standard/b/system/questions/sort`,
        headers: {
          'x-access-token': aiToken,
        },
        json: true,
        body: {
          // jobToken: token,
        },
      };

      const sessionData = await rp(options);
      return sessionData;
    } catch (e) {
      return null;
    }
  }
};
